package fr.iut.tp3.view.components;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import fr.iut.tp3.Context;
import fr.iut.tp3.controller.events.Event;
import fr.iut.tp3.controller.events.EventDispatcher;
import fr.iut.tp3.controller.events.EventListener;
import fr.iut.tp3.view.MainView;

public class MainPanel extends JDesktopPane implements EventListener {

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	private static final long serialVersionUID = 7633131584325491819L;
	private static final int NB_PANELS = 6;
	private Context context;

	private List<JInternalFrame> frames = new ArrayList<JInternalFrame>();

	/**
	 * Panneau principal de l'application.
	 */
	public MainPanel(Context context) {
		this.context = context;
		createAllFrames();
		createTableUtilisateur();
		addComponentListener(new ComponentListener() {

			@Override
			public void componentShown(ComponentEvent arg0) {}
			
			@Override
			public void componentResized(ComponentEvent arg0) {
				positionner();
			}
			
			@Override
			public void componentMoved(ComponentEvent arg0) {}
			
			@Override
			public void componentHidden(ComponentEvent arg0) {}
		});
		EventDispatcher.getInstance().registerListener(this);
	}

	/**
	 * creer la table d'utilisateurs
	 */
	public void createTableUtilisateur(){
		JPanel panelTableUtilisateur1 = new CouleurTableUtilisateurs(context);
		JPanel panelTableUtilisateur2 = new ComboxTableUtilisateurs(context);
		getFrame(5).add(panelTableUtilisateur1);
		getFrame(4).add(panelTableUtilisateur2);
		getFrame(3).add(new TreeUtilisateurs(context.getListeUtilisateurs()));
	}
	/**
	 * Initialise tous les panneaux.
	 */
	private void createAllFrames() {
		for (int i = 0; i < NB_PANELS; i++) {
			addFrame();
		}
		positionner();
	}

	private void addFrame() {
		JInternalFrame frame = new JInternalFrame();
		frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		frame.setResizable(true);
		frame.setVisible(true);
		frames.add(frame);
		add(frame);
	}

	private void positionner() {
		int cols;
		int biggestWidth = getBiggestFrameWidth();
		int biggestHeight = getBiggestFrameHeight();
		if (getParent() != null) {
			cols = getParent().getWidth() / biggestWidth;
		} else {
			cols = MainView.DEFAULT_WIDTH / biggestWidth;
		}
		int currentCol = 0, currentRow = 0;
		for (JInternalFrame frame : frames) {
			frame.setSize(biggestWidth, biggestHeight);
			frame.setLocation(biggestWidth * currentCol, biggestHeight * currentRow);
			currentCol++;
			if (currentCol >= cols) {
				currentCol = 0;
				currentRow++;
			}
		}
	}

	private int getBiggestFrameWidth() {
		int width = 0;
		for (JInternalFrame frame : frames) {
			if (frame.getWidth() > width) {
				width = frame.getWidth();
			}
		}
		return width;
	}

	private int getBiggestFrameHeight() {
		int height = 0;
		for (JInternalFrame frame : frames) {
			if (frame.getHeight() > height) {
				height = frame.getHeight();
			}
		}
		return height;
	}

	public JInternalFrame getFrame(int i) {
		return frames.get(i);
	}

	@Override
	public void onEvent(Event event) {
		switch (event) {
		case NEW_WINDOW:
			addFrame();
			positionner();
			break;
		default:
			break;
		}
	}
}
